﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;

namespace HPIT.RentHouse.Service
{
    public class RolesService : IServiceRoles
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<RolesDTO> GetList(int start, int length, string name, ref int count)
        {
            var db = new RentEntity();
            var bs = new BaseService<T_Roles>(db);
            var query = PredicateExtensions.True<T_Roles>();
            if (!string.IsNullOrEmpty(name))
            {
                query = query.And(e => e.Name.Contains(name));
            }
            var list = bs.GetPagedList(start, length, ref count, query, a => a.Id);
            var result = list.Select(a => new RolesDTO
            {
                Id = a.Id,
                Name = a.Name
            }).ToList();
            return result;
        }
        /// <summary>
        /// 获取角色Id和角色名称
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<RolesDTO> GetList()
        {
            var db = new RentEntity();
            BaseService<T_Roles> bs = new BaseService<T_Roles>(db);
            var list = bs.GetList(r => true).Select(a => new RolesDTO
            {
                Id = a.Id,
                Name=a.Name 
            }).ToList();
            return list;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public AjaxResult Add(RolesEditDTO roles)
        {           
            var db = new RentEntity();
            var bs = new BaseService<T_Roles>(db);
            var bsp = new BaseService<T_Permissions>(db);
            //实例化类对象
            var model = new T_Roles();
            model.Name = roles.Name;
            model.CreateDateTime = DateTime.Now;
            //关联表(T_Permissions)信息的添加
            if (roles.PermissionIds != null && roles.PermissionIds.Count > 0)
            {
                foreach (var id in roles.PermissionIds)
                {
                    T_Permissions permissions = bsp.Get(a => a.Id == id);
                    model.T_Permissions.Add(permissions);
                }
            }
            long roleId = bs.Add(model);
            if (roleId > 0)
            {
                return new AjaxResult(ResultState.Success, "角色添加成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "角色添加失败！");
            }
        }
        /// <summary>
        /// 根据id查询该条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RolesEditDTO Get(long id)
        {
            var db = new RentEntity();
            BaseService<T_Roles> bs = new BaseService<T_Roles>(db);
            //根据id查询该条数据是否存在
            T_Roles model = bs.Get(a => a.Id == id);
            RolesEditDTO dto = new RolesEditDTO();
            if (model != null)
            {
                dto.Id = model.Id;
                dto.Name = model.Name;              
            }
            return dto;
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public AjaxResult Update(RolesEditDTO roles)
        {
            var db = new RentEntity();
            var bs = new BaseService<T_Roles>(db);
            var bsp = new BaseService<T_Permissions>(db);
            //根据id查询该条数据
            var model = bs.Get(e => e.Id == roles.Id);
            model.Name = roles.Name;
            //修改关联表(T_Permissions)中的信息  (先删除表中该条数据已存在的权限id，然后再添加数组里的权限id)
            model.T_Permissions.Clear();
            if (roles.PermissionIds != null && roles.PermissionIds.Count > 0)
            {
                //先通过循环获取用户选中的权限id,再调用添加方法把用户选中的权限id添加进关联表中
                foreach (var id in roles.PermissionIds)
                {
                    T_Permissions per = bsp.Get(a => a.Id == id);
                    model.T_Permissions.Add(per);
                }
            }
            bool res = bs.Update(model);
            if (res)
            {
                return new AjaxResult(ResultState.Success, "角色编辑成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "角色编辑失败！");
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult Delete(long id)
        {
            var db = new RentEntity();
            BaseService<T_Roles> bs = new BaseService<T_Roles>(db);
            //根据id查询该条数据是否存在
            var model = bs.Get(a => a.Id == id);
            if (bs.Delete(model))
            {
                return new AjaxResult(ResultState.Success, "删除角色成功！");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "删除角色失败！");
            }
        }
        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            try
            {
                var db = new RentEntity();
                BaseService<T_Roles> bs = new BaseService<T_Roles>(db);
                //通过循环获取用户选中的多个id,并查询这批数据，实现批量删除
                foreach (var id in ids)
                {
                    var model = bs.Get(a => a.Id == id);
                    bs.Delete(model);
                }
                return new AjaxResult(ResultState.Success, "批量删除成功！");
            }
            catch (Exception)
            {
                return new AjaxResult(ResultState.Error, "批量删除失败！");
            }
        }
    }
}
